package users.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import common.dao.impl.BaseDaoImpl;
import common.util.PageInfoUtil;
import users.dao.IARoleDao;
import users.pojo.ARole;

public class ARoleDaoImpl  extends BaseDaoImpl<ARole>  implements IARoleDao
{

	@Override
	public int saveOne(ARole role)
	{
		/* 准备一个List;条件;就是每一个?对应的值 */
		List<Object> paramsList = new ArrayList<Object>();
		/* sql语句 */
		StringBuilder sb = new StringBuilder();
		sb.append("insert into a_role (name,content,status,createTime,updateTime,pubTime) ");
		sb.append(" values ");
		sb.append(" (?,?,?,?,?,?) ");
		
		paramsList.add(role.getName());
		paramsList.add(role.getContent());
		paramsList.add(role.getStatus());
		paramsList.add(role.getCreateTime());
		paramsList.add(role.getUpdateTime());
		paramsList.add(role.getPubTime());
		/*执行sql语句,把里面的sql语句添加到ps的操作*/
		int res = excuteSql(sb.toString(),paramsList);
		/* 处理主键 */
		int id = (int) paramsList.get(paramsList.size() - 1);
		role.setId(id);
		return res ;
	}

	@Override
	public int updateOne(ARole role)
	{
		/* 准备一个List;条件;就是每一个?对应的值 */
		List<Object> paramsList = new ArrayList<Object>();
		/* sql语句 */
		StringBuilder sb = new StringBuilder();
		sb.append("update a_role set ");
		sb.append(" name = ? ,content = ? ,status = ? ,createTime = ? ,updateTime = ? ,pubTime = ? ");
		sb.append(" where id = ? ");
		
		paramsList.add(role.getName());
		paramsList.add(role.getContent());
		paramsList.add(role.getStatus());
		paramsList.add(role.getCreateTime());
		paramsList.add(role.getUpdateTime());
		paramsList.add(role.getPubTime());
		paramsList.add(role.getId());
		
		/*执行SQL语句*/
		 return  this.excuteSql(sb.toString(),paramsList);
	}

	@Override
	public int deleteOne(Map<String, Object> condMap)
	{
		/* 准备一个List;条件;就是每一个?对应的值 */
		List<Object> paramsList = new ArrayList<Object>();
		/* sql语句 */
		StringBuilder sb = new StringBuilder();
		sb.append("delete from a_role ");
		if(condMap.get("id") != null && !"".equalsIgnoreCase(condMap.get("id") + ""))
		{
			sb.append(" where id = ? ");
			paramsList.add(condMap.get("id"));
		}else
		{
			/* 不传条件不能删除 */
			return 0 ; 
		}
		return this.excuteSql(sb.toString(), paramsList);
	}

	@Override
	public ARole findOne(Map<String, Object> condMap)
	{
		/*准备一个List,就是对应的每一个？都赋值*/
		
		List<Object> paramsList = new ArrayList<Object>();
		/* sql语句 */
		StringBuilder sb = new StringBuilder();
		sb.append("select * from a_role ");
		if(condMap.get("id") != null && !"".equalsIgnoreCase(condMap.get("id") + ""))
		{
			sb.append(" where id = ? ");
			paramsList.add(condMap.get("id"));
		}else
		{
			return null ; 
		}
		return findOneSql(sb.toString(),paramsList,ARole.class);
	}

	
	@Override
	public List<ARole> findCondList(PageInfoUtil pageInfoUtil,Map<String, Object> condMap)
	{
		/*准备一个List,就是对应的每一个？都赋值*/
		List<Object> paramsList = new ArrayList<Object>();
		/* sql语句 */
		StringBuilder sb = new StringBuilder();
		sb.append("select * from a_role where 1=1 ");
		
		/*关键字*/
		if(condMap.get("keyword")!=null &&!"".equalsIgnoreCase(condMap.get("keyword")+""))
		{
			sb.append("and (name link ? or content link ?)");
			paramsList.add("%"+condMap.get("keyword")+"%");
			paramsList.add("%"+condMap.get("keyword")+"%");
		}
		/*状态*/
		if(condMap.get("status")!=null &&!"".equalsIgnoreCase(condMap.get("status")+""))
		{
			sb.append("and status = ?");
			paramsList.add(condMap.get("status"));
		}
		/*时间*/
		if(condMap.get("st") instanceof Date && condMap.get("ed") instanceof Date )
		{
			sb.append(" and pubTime >= ? and pubTime < ? ");
			paramsList.add(condMap.get("st"));
			paramsList.add(condMap.get("ed"));
		}
		/*排序*/
		if("idDesc".equalsIgnoreCase(condMap.get("orderBy")+""))
		{
			sb.append("order by id desc");
		}else
		{
			sb.append("order by pubTime desc ");
		}
		if(pageInfoUtil !=null)
		{
			return this.findListSql(pageInfoUtil, sb.toString(), paramsList,ARole.class);
		}
		return this.findListSql(sb.toString(), paramsList, ARole.class);
		
	}
	
}
